package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.model.Category;
import com.cskaoyan.model.CategoryExample;
import com.cskaoyan.model.vo.BaseRespVO;
import com.cskaoyan.model.vo.WxCatalogIndexVO;
import com.cskaoyan.service.WxCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：王李轶群
 * @description：TODO
 * @date ：5/9/22 8:25 PM
 */
@Service
public class WxCatalogServiceImpl implements WxCatalogService {
    @Autowired
    CategoryMapper categoryMapper;
    @Override
    public BaseRespVO<WxCatalogIndexVO> catalogIndex() {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria categoryExampleCriteria = categoryExample.createCriteria();
        categoryExampleCriteria.andLevelEqualTo("L1");
        categoryExampleCriteria.andDeletedEqualTo(false);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        WxCatalogIndexVO wxCatalogIndexVO = new WxCatalogIndexVO();
        wxCatalogIndexVO.setCategoryList(categoryList);
        if(categoryList!=null){
            Category currentCategory = categoryList.get(0);
            Integer id = currentCategory.getId();
            CategoryExample categoryExample1 = new CategoryExample();
            CategoryExample.Criteria criteria = categoryExample1.createCriteria();
            criteria.andPidEqualTo(id);
            criteria.andDeletedEqualTo(false);
            List<Category> currentSubCategory = categoryMapper.selectByExample(categoryExample1);
            wxCatalogIndexVO.setCurrentCategory(currentCategory);
            wxCatalogIndexVO.setCurrentSubCategory(currentSubCategory);
        }
        BaseRespVO<WxCatalogIndexVO> wxCatalogIndexVOBaseRespVO = new BaseRespVO<>();
        wxCatalogIndexVOBaseRespVO.setData(wxCatalogIndexVO);
        wxCatalogIndexVOBaseRespVO.setErrmsg("成功");
        wxCatalogIndexVOBaseRespVO.setErrno(0);
        return wxCatalogIndexVOBaseRespVO;
    }

    @Override
    public BaseRespVO<WxCatalogIndexVO> catalogCurrent(Integer id) {
        Category currentCategory = categoryMapper.selectByPrimaryKey(id);
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria categoryExampleCriteria = categoryExample.createCriteria();
        categoryExampleCriteria.andPidEqualTo(id);
        categoryExampleCriteria.andDeletedEqualTo(false);
        List<Category> currentSubCategory = categoryMapper.selectByExample(categoryExample);
        WxCatalogIndexVO wxCatalogIndexVO = new WxCatalogIndexVO();
        wxCatalogIndexVO.setCurrentCategory(currentCategory);
        wxCatalogIndexVO.setCurrentSubCategory(currentSubCategory);
        BaseRespVO<WxCatalogIndexVO> wxCatalogIndexVOBaseRespVO = new BaseRespVO<>();
        wxCatalogIndexVOBaseRespVO.setErrno(0);
        wxCatalogIndexVOBaseRespVO.setData(wxCatalogIndexVO);
        wxCatalogIndexVOBaseRespVO.setErrmsg("成功");
        return wxCatalogIndexVOBaseRespVO;
    }
}
